<template>
  <span
    class="van-sku-row__item"
    :class="{
      'van-sku-row__item--active': isChoosed,
      'van-sku-row__item--disabled': !isChoosable
    }"
    @click="onSelect"
  >
    {{ skuValue.name }}
  </span>
</template>

<script>
import { isSkuChoosable } from '../utils/sku/skuHelper';

export default {
  name: 'sku-row-item',
  props: {
    skuEventBus: Object,
    skuValue: Object,
    skuList: Array,
    selectedSku: Object,
    skuKeyStr: String
  },

  computed: {
    isChoosed() {
      return this.skuValue.id === this.selectedSku[this.skuKeyStr];
    },

    isChoosable() {
      return isSkuChoosable(this.skuList, this.selectedSku, {
        key: this.skuKeyStr,
        valueId: this.skuValue.id
      });
    }
  },
  methods: {
    onSelect() {
      if (this.isChoosable) {
        this.$emit('sku:select', {
          ...this.skuValue,
          skuKeyStr: this.skuKeyStr
        });
      }
    }
  }
};
</script>

<style lang="scss">
  .van-sku-row__item {
    display: inline-block;
    padding: 5px 9px;
    margin: 0 10px 10px 0;
    height: 28px;
    min-width: 52px;
    line-height: 16px;
    font-size: 12px;
    color: #323233;
    text-align: center;
    border: 1px solid #969799;
    border-radius: 3px;
    box-sizing: border-box;
  }
  .van-sku-row__item--active {
    color: #fff;
    border-color: #f44;
    background: #f44;
  }
  .van-sku-row__item--disabled {
    background: #e8e8e8;
    border-color: #ebedf0;
    color: #c8c9cc;
  }
</style>